package exam;



import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.exam.domain.Instructor;


public class TemplateServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public TemplateServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		String submit1=request.getParameter("submit");
		TemplateBean template=new TemplateBean();
		
		Instructor ins=(Instructor)request.getSession().getAttribute("insinfo");
		
		
		if(submit1.equals("true"))
		{
		

			String _courseid=request.getParameter("course_id");
			String choice_question_score=request.getParameter("choice_question_score");
			String sub_question_score=request.getParameter("sub_question_score");

			String[] cata_id=request.getParameterValues("catalog_id");
			String[] choice_num=request.getParameterValues("number_of_choice");
			String[] sub_num=request.getParameterValues("number_of_sub");

			String instructor_id=ins.getI_id();
			
			

			template.setCourseId(_courseid);
			template.setInstructorId(instructor_id);
			template.setChoiceScore(choice_question_score);
			template.setSub_score(sub_question_score);
			template.setCatalog_id(cata_id);
			template.setChoice_No(choice_num);
			template.setSub_No(sub_num);

			
			String type=request.getParameter("type");
			if(type.equals("add"))
			{
				String tp_name=request.getParameter("template_name");
				template.setTemplatename(tp_name);
				template.addtemplate();
				response.sendRedirect(request.getContextPath()+"/tea_addtemplate.jsp");
			}
			else if(type.equals("modify"))
			{
				String _templateid=request.getParameter("template_id");
				template.setTemplateid(_templateid);
				template.modifytemplate();
				response.sendRedirect(request.getContextPath()+"/tea_templatemanage.jsp");
			}
		}
		else
		{	
			String type = request.getParameter("type");
			
			if(type.equals("add"))
			{	
				String _courseid=request.getParameter("course_id");
				String isrefresh="true";
				template.setCourseId(_courseid);
				List<Catalog> cat= template.refresh_catalog();
				request.getSession().setAttribute("list_of_cat", cat);
				request.getSession().setAttribute("course_id", _courseid);
				request.getSession().setAttribute("is_refresh",isrefresh );
		
				response.sendRedirect(request.getContextPath()+"/tea_addtemplate.jsp");
			}
			
			else if(type.equals("modify"))
			{
				String act=request.getParameter("act");
				if(act.equals("findtemp"))
				{
					String _courseid=request.getParameter("course_id");
					String _instructorid=ins.getI_id();
					template.setCourseId(_courseid);
					template.setInstructorId(_instructorid);
					List<Tempinfo> temp= template.find_temp();
					String foundtemp="true";
					request.getSession().setAttribute("list_of_temp", temp);
					request.getSession().setAttribute("course_id", _courseid);
					request.getSession().setAttribute("foundtemp",foundtemp );
					response.sendRedirect(request.getContextPath()+"/tea_templatemanage.jsp");

				}
				else if(act.equals("findcat"))
				{
					String template_id=request.getParameter("template_id");
					String _courseid=request.getParameter("course_id");
					template.setTemplateid(template_id);
					template.setCourseId(_courseid);
					List<Catalog> cat= template.refresh_catalog();
					List<String> chosen_Cat=template.find_catalog();
					String isrefresh="true";
					request.getSession().setAttribute("list_of_cat", cat);
					request.getSession().setAttribute("course_id", _courseid);
					request.getSession().setAttribute("chosen_cat",chosen_Cat);
					request.getSession().setAttribute("is_refresh",isrefresh);
					response.sendRedirect(request.getContextPath()+"/tea_templatemanage.jsp");
				}
				else if(act.equals("del_temp"))
				{
					String template_id=request.getParameter("template_id");
					template.setTemplateid(template_id);
					template.del_temp();
					response.sendRedirect(request.getContextPath()+"/tea_templatemanage.jsp");
				}
			}
		}

	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
